<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AlexNet图像分类</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .upload-area {
            border: 2px dashed #ccc;
            border-radius: 5px;
            padding: 40px;
            text-align: center;
            margin-bottom: 20px;
            cursor: pointer;
        }
        .upload-area:hover {
            border-color: #007bff;
        }
        .upload-area.dragover {
            border-color: #007bff;
            background-color: #f8f9fa;
        }
        #imagePreview {
            max-width: 100%;
            max-height: 300px;
            margin: 20px 0;
            display: none;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .probability-bar {
            background-color: #e9ecef;
            border-radius: 3px;
            margin: 5px 0;
            overflow: hidden;
        }
        .probability-fill {
            background-color: #007bff;
            height: 20px;
            color: white;
            padding-left: 10px;
            line-height: 20px;
            font-size: 12px;
        }
        .probability-info {
        display: flex;
        justify-content: space-between;
        padding: 0 10px;
        font-size: 12px;
        color: #333; /* 设置字体颜色 */
        font-weight: bold;
    }

    .probability-bar {
        background-color: #e9ecef;
        border-radius: 3px;
        margin: 5px 0;
        overflow: hidden;
    }

    .probability-fill {
        background-color: #007bff;
        height: 20px;
        color: white; /* 进度条上的文字颜色 */
        padding-left: 10px;
        line-height: 20px;
        font-size: 12px;
        transition: width 0.3s ease;
    }
    </style>
</head>
<body>
    <div class="container">
        <h1>AlexNet图像分类</h1>
        <p>上传一张图像，模型将预测其类别</p>

        <div class="upload-area" id="uploadArea">
            <p>点击选择图像或拖拽图像到这里</p>
            <input type="file" id="fileInput" accept="image/*" style="display: none;">
        </div>

        <img id="imagePreview" alt="预览图像">

        <button id="predictBtn" disabled>开始预测</button>

        <div id="result" class="result"></div>
    </div>

    <script>
        const fileInput = document.getElementById('fileInput');
        const uploadArea = document.getElementById('uploadArea');
        const imagePreview = document.getElementById('imagePreview');
        const predictBtn = document.getElementById('predictBtn');
        const resultDiv = document.getElementById('result');

        // 点击上传区域触发文件选择
        uploadArea.addEventListener('click', () => {
            fileInput.click();
        });

        // 拖拽功能
<!--        uploadArea.addEventListener('dragover', (e) => {-->
<!--            e.preventDefault();-->
<!--            uploadArea.classList.add('dragover');-->
<!--        });-->

        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });

        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                handleFile(files[0]);
            }
        });

        // 文件选择变化
        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                handleFile(e.target.files[0]);
            }
        });

        function handleFile(file) {
            if (!file.type.startsWith('image/')) {
                showResult('请选择图像文件', 'error');
                return;
            }

            // 显示预览
            const reader = new FileReader();
            reader.onload = (e) => {
                imagePreview.src = e.target.result;
                imagePreview.style.display = 'block';
                predictBtn.disabled = false;
                resultDiv.style.display = 'none';
            };
            reader.readAsDataURL(file);
        }

        // 预测按钮点击
        predictBtn.addEventListener('click', async () => {
            const file = fileInput.files[0];
            if (!file) return;

            predictBtn.disabled = true;
            predictBtn.textContent = '预测中...';

            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch('/predict', {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();

                if (response.ok) {
                    showPredictionResult(data);
                } else {
                    showResult(data.error || '预测失败', 'error');
                }
            } catch (error) {
                showResult('网络错误: ' + error.message, 'error');
            } finally {
                predictBtn.disabled = false;
                predictBtn.textContent = '开始预测';
            }
        });

        function showPredictionResult(data) {
            let html = `
                <div class="success">
                    <h3>预测结果</h3>
                    <p><strong>类别:</strong> ${data.class_name} (ID: ${data.class_id})</p>
                    <p><strong>置信度:</strong> ${(data.confidence * 100).toFixed(2)}%</p>
                    <h4>所有类别概率:</h4>
            `;

            // 按概率排序
            const sortedProbs = Object.entries(data.all_probabilities)
                .sort((a, b) => b[1] - a[1]);

            sortedProbs.forEach(([className, prob]) => {
                const percentage = (prob * 100).toFixed(2);
                html += `
                    <div class="probability-bar">
                        <div class="probability-info">
                            <span>${className}</span>
                            <span>${percentage}%</span>
                        </div>
                        <div class="probability-fill" style="width: ${percentage}%">
                            <!-- 进度条上的文字 -->
                        </div>
                    </div>
                `;
            });

            html += '</div>';
            resultDiv.innerHTML = html;
            resultDiv.className = 'result success';
            resultDiv.style.display = 'block';
        }

        function showResult(message, type) {
            resultDiv.innerHTML = `<div class="${type}">${message}</div>`;
            resultDiv.className = `result ${type}`;
            resultDiv.style.display = 'block';
        }
    </script>
</body>
</html>